#include "showimage.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>

showImage::showImage(QWidget *parent) : QWidget(parent)
{
    isHorizon = true;
    m_pos= 0;
}

void showImage:: showImg(QImage img)
{
    m_img = img.copy();
    update();
}

void showImage::  paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
    QPainter painter(this);
    painter.setRenderHint ( QPainter::Antialiasing, true );
    painter.save();

    painter.drawImage ( 0, 0, m_img );
//    qDebug() << "m_img.size():" << m_img.size();
    if(isHorizon)
    {
        painter.drawLine(QPoint(0,m_pos), QPoint(m_img.width(), m_pos));
    }
    else
    {
        painter.drawLine(QPoint(m_pos, 0), QPoint(m_pos, m_img.height()));
    }

    painter.restore();

}

void showImage:: mousePressEvent(QMouseEvent *event)
{
    QPoint Pos = event->pos();
    qDebug() << "Pos:" << Pos;
    if( event->button() == Qt::RightButton )    // 竖线
    {
        isHorizon = false;
        m_pos = Pos.x();
    }
    else if( event->button() == Qt::LeftButton )
    {
        isHorizon = true;
        m_pos = Pos.y();
    }
    QWidget::mousePressEvent(event);
    update();
}

int showImage::getResult()
{
    return m_pos;
}
